package com.cxrj.zhsq.userManage.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.cxrj.zhsq.userManage.dao.MessageRecipientMapper;
import com.cxrj.zhsq.userManage.dao.SystemMessageMapper;
import com.cxrj.zhsq.userManage.entity.MessageRecipient;
import com.cxrj.zhsq.userManage.entity.SystemMessage;
import com.cxrj.zhsq.userManage.service.SystemMessageService;
import com.cxrj.zhsq.utils.HtmlUtil;
import com.cxrj.zhsq.utils.IpUtil;
import com.cxrj.zhsq.utils.SqlCondition;

@Service
public class SystemMessageServiceImpl implements SystemMessageService {
	@Resource
	private SystemMessageMapper systemMessageMapper;

	@Resource
	private MessageRecipientMapper messageRecipientMapper;

	@Override
	public Map<String, Object> selectSystemMessagePage(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = SqlCondition.getBaseCondition(request, response);
		int count = systemMessageMapper.getSystemMessageCount(sqlMap);
		List<SystemMessage> list = systemMessageMapper.selectSystemMessagePage(sqlMap);
		resultMap.put("total", count);
		resultMap.put("recordsFiltered", count);
		resultMap.put("rows", list);
		return resultMap;
	}

	@Override
	public Map<String, Object> insertSystemMessage(SystemMessage systemMessage, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap=new HashMap<>();
		systemMessage.setCreateTime(new Date());
		systemMessage.setSmId(UUID.randomUUID().toString());
		
		int a=systemMessageMapper.insertSelective(systemMessage);
		
		String[] ids=systemMessage.getUser_ids().split(",");
		MessageRecipient messageRecipient=new MessageRecipient();
		messageRecipient.setSmId(systemMessage.getSmId());
		for(int i=0;i<ids.length;i++){
			messageRecipient.setUserId(ids[i]);
			messageRecipient.setSrId(UUID.randomUUID().toString());
			messageRecipient.setCreateTime(new Date());
			messageRecipient.setState(0);
			messageRecipientMapper.insertSelective(messageRecipient);
		}
		resultMap.put("result", a);
		return resultMap;
	}

	@Override
	public Map<String, Object> selectSystemMessage(String userId, String dataIndex, String pageCount, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("userId", userId);
		sqlMap.put("start", Integer.parseInt(dataIndex));
		sqlMap.put("length", Integer.parseInt(pageCount));
		List<SystemMessage> list = systemMessageMapper.selectSystemMessage(sqlMap);
		resultMap.put("list", list);
		return resultMap;
	}

	@Override
	public Map<String, Object> selectSystemMessageById(String id, String userId, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		SystemMessage systemMessage = systemMessageMapper.selectByPrimaryKey(id);
		if (systemMessage.getSystemMessageContent() != null && !systemMessage.getSystemMessageContent().equals("")) {
			systemMessage.setSystemMessageContent(HtmlUtil.getAbsSource(systemMessage.getSystemMessageContent(), "http://" + IpUtil.getIpAddr(request) + ":" + request.getServerPort() + "/"));
		}
		if(userId != null) {
			Map<String, Object> sqlMap=new HashMap<String, Object>();
			sqlMap.put("userId", userId);
			sqlMap.put("smId", id);
			messageRecipientMapper.updateByRead(sqlMap);
		}
		resultMap.put("systemMessage", systemMessage);
		return resultMap;
	}

	@Override
	public Map<String, Object> updateSystemMessage(SystemMessage systemMessage, HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> reultMap=new HashMap<>();
		int a=systemMessageMapper.updateByPrimaryKeySelective(systemMessage);
		reultMap.put("result", a);
		return reultMap;
	}

	@Override
	public Map<String, Object> deleteSystemMessage(String smId, HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> resultMap=new HashMap<>();
		int a=systemMessageMapper.deleteByPrimaryKey(smId);
		int b=messageRecipientMapper.deleteBySmid(smId);
		resultMap.put("result", 0);
		if (a>0&b>0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}
}
